#include "tools/param.hpp"

namespace toolsLib
{

void getParam::loadYamlFile(const std::string& file,
                            const std::string& funcName,
                            const int& line,
                            const std::string& paramPath)
{
    try {
        // YAML::LoadFile 会自己处理文件的打开和关闭
        config = YAML::LoadFile(paramPath);
    } catch (const YAML::Exception& e) {
        Logger::getInstance().log(file, funcName, line, 3, 
                                  "Unable to load YAML file: " + paramPath + " Error: " + e.what());
    } catch (const std::exception& e) {
        Logger::getInstance().log(file, funcName, line, 3, 
                                  "Unable to load YAML file: " + paramPath + " Error: " + e.what());
    }
}

void getParam::getParameter(const std::string& file,
                            const std::string& funcName,
                            const int& line,
                            const std::string& param,
                            int& value)
{
    try {
        if (!config[param]) {
            Logger::getInstance().log(file, funcName, line, 3, 
                                      "Parameter not found: " + param);
            return;  // value 保持原值
        }
        value = config[param].as<int>();
    } catch (const YAML::Exception& e) {
        Logger::getInstance().log(file, funcName, line, 3, 
                                  "Error getting parameter " + param + ": " + e.what());
    }
}

void getParam::getParameter(const std::string& file,
                            const std::string& funcName,
                            const int& line,
                            const std::string& param,
                            double& value)
{
    try {
        if (!config[param]) {
            Logger::getInstance().log(file, funcName, line, 3, 
                                      "Parameter not found: " + param);
            return;  // value 保持原值
        }
        value = config[param].as<double>();
    } catch (const YAML::Exception& e) {
        Logger::getInstance().log(file, funcName, line, 3, 
                                  "Error getting parameter " + param + ": " + e.what());
    }
}

void getParam::getParameter(const std::string& file,
                            const std::string& funcName,
                            const int& line,
                            const std::string& param,
                            bool& value)
{
    try {
        if (!config[param]) {
            Logger::getInstance().log(file, funcName, line, 3, 
                                      "Parameter not found: " + param);
            return;  // value 保持原值
        }
        value = config[param].as<bool>();
    } catch (const YAML::Exception& e) {
        Logger::getInstance().log(file, funcName, line, 3, 
                                  "Error getting parameter " + param + ": " + e.what());
    }
}

void getParam::getParameter(const std::string& file,
                            const std::string& funcName,
                            const int& line,
                            const std::string& param,
                            std::string& value)
{
    try {
        if (!config[param]) {
            Logger::getInstance().log(file, funcName, line, 3, 
                                      "Parameter not found: " + param);
            return;  // value 保持原值
        }
        value = config[param].as<std::string>();
    } catch (const YAML::Exception& e) {
        Logger::getInstance().log(file, funcName, line, 3, 
                                  "Error getting parameter " + param + ": " + e.what());
    }
}

}  // namespace toolsLib